<template>
  <div>
    <div class="info">
      <h2>用户资料</h2>
      <el-button type="primary" @click="findWorks">查看作品</el-button>
    </div>
    <div class="card-feel">
      <div class="avator">
        <img v-if="info.avator" :src="info.avator" alt="" />
        {{ info.nickname }}
      </div>
      <div class="info-item">
        <span>地区：</span>
        <span>{{ info.province }}</span>
      </div>
      <div class="info-item">
        <span>邮箱：</span>
        <span>{{ info.email }}</span>
      </div>
      <div class="info-item">
        <span>性别：</span>
        <span>{{ info.sex }}</span>
      </div>
      <div class="info-item">
        <span>生日：</span>
        <span>{{ info.birthday }}</span>
      </div>
      <div class="info-item">
        <span>手机号码：</span>
        <span>{{ info.phone }}</span>
      </div>
      <div class="info-item">
        <span>职业：</span>
        <span>{{ info.profession }}</span>
      </div>
      <div class="info-item">
        <span>个性签名：</span>
        <span>{{ info.intro }}</span>
      </div>
    </div>
    <h2>用户平台信息</h2>
    <div class="card-feel">
      <div class="info-item">
        <span>使用平台类型：</span>
        <span>{{ info.platform }}</span>
      </div>
      <div class="info-item">
        <span>注册来源平台及渠道：</span>
        <span>{{ info.appid }}</span>
      </div>
      <div class="info-item">
        <span>认证身份：</span>
        <span v-if="info.identification" class="identification">
          <span v-if="info.identification.jmPerson == '积幕个人认证'">
            {{ info.identification.jmPerson }}、
          </span>
          <span v-if="info.identification.jmTeam == '积幕团队认证'">
            {{ info.identification.jmTeam }}、
          </span>
          <span v-if="info.identification.ktTeacher == '讲师认证'">
            {{ info.identification.ktTeacher }}
          </span>
          <el-button type="primary" @click="certificationShow = true">运营认证</el-button>
        </span>
      </div>
      <div class="info-item">
        <span>认证等级：</span>
        <span>{{ info.level }}</span>
      </div>
      <div class="info-item">
        <el-button type="primary" @click="setClaim">设置认领的作品和职务</el-button>
      </div>
    </div>
    <h2>用户会员信息</h2>
    <div class="card-feel">
      <div v-for="(item, index) in info.vipInfo" :key="index">
        <div class="info-item info-item-longer">
          <span>会员类型：</span>
          <span>{{ item.vipName }}</span>
        </div>
        <div class="info-item info-item-longer">
          <span>加入有课会员时间：</span>
          <span>{{ item.indateStart }}</span>
        </div>
        <div class="info-item info-item-longer">
          <span>有课会员到期日：</span>
          <span>{{ item.indateEnd }}</span>
        </div>
        <div class="info-item info-item-longer">
          <span>累计购买有课会员卡金额：</span>
          <span>{{ item.amount }}元</span>
        </div>
      </div>
    </div>
    <h2>用户状态</h2>
    <div class="card-feel">
      <div class="info-item" v-if="info.userActionInfo">
        <span>用户状态：</span>
        <span>{{ status }}</span>
      </div>
      <div class="info-item" v-if="info.userActionInfo">
        <span>黑名单权限设置：</span>
        <el-select
          v-model="blacklist"
          placeholder="黑名单设置"
          type="primary"
          @change="setBlacklist"
        >
          <el-option
            v-for="item in blacklistOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
    </div>
    <h2>用户权重</h2>
    <div class="card-feel">
      <div class="info-item">
        <span>用户权重配置：</span>
        <el-select
          v-model="info.weight"
          placeholder="权重设置"
          type="primary"
          @change="weightChange"
        >
          <el-option
            v-for="item in weightSl"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="info-item  gr">
        <span>用户主页样式：</span>
        <el-radio-group v-model="info.homepageStyle" @change="homepageStyleChange">
          <el-radio :label="0">个人用户主页</el-radio>
          <el-radio :label="1">企业用户主页</el-radio>
        </el-radio-group>
      </div>
    </div>
    <h2>有课学习数据</h2>
    <div class="card" v-if="info.learnInfo">
      <el-card class="box-card">
        <div class="carddiv">
          <p class="cardName">今日时长</p>
          <p class="cardTime">{{ info.learnInfo.todayHours }}小时</p>
        </div>
      </el-card>
      <el-card class="box-card">
        <div class="carddiv">
          <p class="cardName">累计学习时长</p>
          <p class="cardTime">{{ info.learnInfo.learnHours }}小时</p>
        </div>
      </el-card>
      <el-card class="box-card">
        <div class="carddiv">
          <p class="cardName">完成课程数</p>
          <p class="cardTime">{{ info.learnInfo.courseCnt }}个</p>
        </div>
      </el-card>
      <el-card class="box-card">
        <div class="carddiv">
          <p class="cardName">坚持学习天数</p>
          <p class="cardTime">{{ info.learnInfo.keepDays }}天</p>
        </div>
      </el-card>
      <el-card class="box-card">
        <div class="carddiv">
          <p class="cardName">关注数</p>
          <p class="cardTime">{{ info.learnInfo.followCnt }}个</p>
        </div>
      </el-card>
    </div>
    <h2>积幕创作数据</h2>
    <el-date-picker
      v-model="dateVal"
      type="daterange"
      range-separator="-"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      @change="dateChange"
      format="yyyy-MM-dd"
      value-format="yyyy-MM-dd"
    ></el-date-picker>
    <div class="card" v-if="info.jmStatisInfo">
      <el-card class="box-card">
        <div class="carddiv">
          <p class="cardName">作品数</p>
          <p class="cardTime">{{ info.jmStatisInfo.workCnt }}个</p>
        </div>
      </el-card>
      <el-card class="box-card">
        <div class="carddiv">
          <p class="cardName">粉丝量</p>
          <p class="cardTime">{{ info.jmStatisInfo.fansCnt }}次</p>
        </div>
      </el-card>
      <el-card class="box-card">
        <div class="carddiv">
          <p class="cardName">浏览数</p>
          <p class="cardTime">{{ info.jmStatisInfo.viewCnt }}次</p>
        </div>
      </el-card>
      <el-card class="box-card">
        <div class="carddiv">
          <p class="cardName">被点赞数</p>
          <p class="cardTime">{{ info.jmStatisInfo.likeCnt }}次</p>
        </div>
      </el-card>
      <el-card class="box-card">
        <div class="carddiv">
          <p class="cardName">被评论数</p>
          <p class="cardTime">{{ info.jmStatisInfo.commentCnt }}次</p>
        </div>
      </el-card>
      <el-card class="box-card">
        <div class="carddiv">
          <p class="cardName">被收藏数</p>
          <p class="cardTime">{{ info.jmStatisInfo.favoredCnt }}次</p>
        </div>
      </el-card>
      <el-card class="box-card">
        <div class="carddiv">
          <p class="cardName">被分享数</p>
          <p class="cardTime">{{ info.jmStatisInfo.shareCnt }}个</p>
        </div>
      </el-card>
    </div>
    <h2>用户是否可获得徽章</h2>
    <div class="card-feel">
      <p>设为不可获得后，已获得的徽章将保留，不会再获得新徽章</p>
      <div>
        <el-radio-group v-model="badgeInfo.badgeEnable" @change="badgeChange">
          <el-radio :label="1">可获得</el-radio>
          <el-radio :label="0">不可获得</el-radio>
        </el-radio-group>
      </div>
    </div>
    <h2>已获得的徽章</h2>
    <div class="card-feel">
      <p>
        用户设为不可获得徽章后，若要删除已获得的徽章，可使用删除功能；若用户为可获得徽章状态，则删除后仍将重新获得
      </p>
      <div class="badge-box" v-if="badgeInfo.badges && badgeInfo.badges.length">
        <div class="badge-item" v-for="(item, index) in badgeInfo.badges" :key="index">
          <span class="title">{{ item.title }}</span>
          <span class="created-at">( {{ item.createdAt }} )</span>
          <el-button type="danger" @click="delBadge(item)">删除</el-button>
        </div>
      </div>
    </div>

    <el-dialog :visible="certificationShow" :before-close="handleClose" width="500px">
      <el-form>
        <el-form-item><span>幕童有课</span></el-form-item>
        <el-form-item><el-checkbox v-model="checked1">讲师</el-checkbox></el-form-item>
        <el-form-item><span>积幕</span></el-form-item>
        <el-form-item>
          <el-checkbox v-model="checked2">积幕个人认证</el-checkbox>
          <el-checkbox v-model="checked3">积幕团队认证</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-input placeholder="认证描述，可选填" v-model="text"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input placeholder="认证称号" v-model="profession"></el-input>
        </el-form-item>
        <el-form-item><span>入行时间</span></el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="value3"
            type="year"
            placeholder="入行或者机构成立时间，选填"
            value-format="yyyy"
          ></el-date-picker>
        </el-form-item>
        <div>
          <el-button type="primary" @click="confirm">确定</el-button>
          <el-button @click="handleClose">取消</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import EditMixins from 'mixins/Edit';
  export default {
    mixins: [EditMixins],
    data() {
      return {
        checked1: false,
        checked2: false,
        checked3: false,
        ktDisabled: 0,
        ktComment: 0,
        jmComment: 0,
        jmDisabled: 0,
        status: '',
        certificationShow: false,
        text: '',
        profession: '',
        value3: '',
        blacklistOptions: [
          { value: 'normal', label: '正常' },
          { value: 'prohibit', label: '禁止访问' },
          { value: 'prohibitComment', label: '禁止评论' },
        ],
        blacklist: '',
        // 时间筛选
        dateVal: '',
        // 用户详情的权重
        weightSl: [
          {
            label: '普通用户权重',
            value: 0,
          },
          {
            label: '重点用户权重',
            value: 1,
          },
        ],
        // 用户的徽章相关信息
        badgeInfo: {},
      };
    },

    methods: {
      // 删除徽章
      delBadge(item) {
        this.$confirm('确定删除该徽章吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            this.$service
              .UserBadgeDelete({ badgeLevelId: item.badgeLevelId, uid: this.$route.params.uuid })
              .then((res) => {
                this.$message.success('操作成功');
                this.getInfo()
              });
          })
          .catch(() => {});
      },

      // 设置徽章的可获与不可获
      badgeChange(val) {
        console.log(val);
        let _ = '';
        _ = val ? this.$service.UserBadgeEnable : this.$service.UserBadgeDisable;
        _({ uid: this.$route.params.uuid }).then((res) => {
          this.$message.success('操作成功');
          this.getInfo()
        });
      },

      // 获得徽章的相关信息
      getBadge() {
        this.$service.UserBadgeInfo({ uid: this.$route.params.uuid }).then((res) => {
          this.badgeInfo = res;
        });
      },

      // 设置用户主页样式
      homepageStyleChange(val) {
        this.$service
          .UserSetHomepageStyle({
            style: val,
            uuid: this.$route.params.uuid,
          })
          .then((res) => {
            this.$message.success('操作成功');
          });
      },
      // 设置权重
      weightChange(val) {
        this.$service
          .UserSetWeight({
            weight: val,
            uuid: this.$route.params.uuid,
          })
          .then((res) => {
            this.$message.success('操作成功');
          });
      },
      // 时间筛选
      dateChange() {
        this.$service
          .UserJmStatis({
            startTime: this.dateVal ? this.dateVal[0] : '',
            endTime: this.dateVal ? this.dateVal[1] : '',
            uuid: this.$route.params.uuid,
          })
          .then((res) => {
            this.info.jmStatisInfo = res;
          });
      },
      // 查看作品跳转到jimu相应页面
      findWorks() {
        window.open(
          `https://${process.env.NODE_ENV === 'development' ? 'dev-' : ''}jimu.movtile.com/space/${
            this.info.uuid
          }/works`,
        );
      },
      getInfo() {
        if (this.$route.name != 'UserManagerDetail' || this.infoGetting) return;
        this.infoGetting = true;
        this.$service
          .UserInfo({ uuid: this.$route.params.uuid })
          .then((res) => {
            this.info = { ...res };
            this.info.sex = this.$ENUMS.sex[this.info.sex];
            this.info.level = this.$ENUMS.level[this.info.level];

            this.text = this.info.identification.remark || '';
            this.profession = this.info.identification.profession || '';
            this.value3 = this.info.identification.startYear
              ? this.info.identification.startYear + ''
              : '';

            if (this.info.identification.jmPerson == 1) {
              this.checked2 = true;
              this.info.identification.jmPerson = '积幕个人认证';
            }
            if (this.info.identification.jmTeam == 1) {
              this.checked3 = true;
              this.info.identification.jmTeam = '积幕团队认证';
            }
            if (this.info.identification.ktTeacher == 1) {
              this.checked1 = true;
              this.info.identification.ktTeacher = '讲师认证';
            }
            if (
              this.info.userActionInfo.jmComment == 0 &&
              this.info.userActionInfo.jmDisabled == 0 &&
              this.info.userActionInfo.ktComment == 0 &&
              this.info.userActionInfo.ktDisabled == 0
            ) {
              this.jmComment = 0;
              this.jmDisabled = 0;
              this.ktComment = 0;
              this.ktDisabled = 0;
              this.status = '正常';
            }
            if (
              this.info.userActionInfo.jmComment == 1 &&
              this.info.userActionInfo.jmDisabled == 1 &&
              this.info.userActionInfo.ktComment == 1 &&
              this.info.userActionInfo.ktComment == 1
            ) {
              this.jmComment = 1;
              this.jmDisabled = 1;
              this.ktComment = 1;
              this.ktDisabled = 1;
              this.status = '禁止';
            }

            // 这里获得数据之后再去获得徽章相关的信息
            this.getBadge();
          })
          .finally(() => {
            this.infoGetting = false;
          });
      },
      handleClose() {
        this.certificationShow = false;
      },
      confirm() {
        var jmPerson = 0;
        var jmTeam = 0;
        var ktTeacher = 0;
        if (this.checked1) {
          this.info.identification.ktTeacher = '讲师认证';
          ktTeacher = 1;
        }
        if (this.checked2) {
          this.info.identification.jmPerson = '积幕个人认证';
          jmPerson = 1;
        }
        if (this.checked3) {
          this.info.identification.jmTeam = '积幕团队认证';
          jmTeam = 1;
        }
        this.$service
          .UserIdentify({
            jmPerson: jmPerson,
            jmTeam: jmTeam,
            ktTeacher: ktTeacher,
            remark: this.text,
            profession: this.profession,
            startYear: this.value3,
            uid: this.info.uuid,
          })
          .then((res) => {
            this.certificationShow = false;
            this.getInfo();
          });
      },
      setBlacklist(val) {
        if (val == 'normal') {
          this.status = '正常';
          this.jmComment = 0;
          this.jmDisabled = 0;
          this.ktComment = 0;
          this.ktDisabled = 0;
        } else {
          this.status = '禁止';
          this.jmComment = 1;
          this.jmDisabled = 1;
          this.ktComment = 1;
          this.ktDisabled = 1;
        }
        this.$service
          .UserDisable({
            jmComment: this.jmComment,
            jmDisabled: this.jmDisabled,
            ktComment: this.ktComment,
            ktDisabled: this.ktDisabled,
            uuid: this.info.uuid,
          })
          .then((res) => {
            this.$message.success('操作成功');
          });
      },
      //设置作品认领
      setClaim() {
        const uuid = this.info.uuid;
        this.$tabs.open({ name: 'UserWorkList', params: { uuid } });
      },
    },
  };
</script>
<style lang="less" scoped>
  .info {
    display: flex;
    justify-content: space-between;
  }
  h2 {
    padding-left: 20px;
    border-left: 5px solid #1d6be0;
    line-height: 1;
    margin-bottom: 10px;
  }
  .card {
    display: flex;
    width: 100%;
    flex-wrap: wrap;
    align-content: space-between;
  }
  .box-card {
    width: 15%;
    height: 150px;
    text-align: center;
    margin-bottom: 10px;
    margin-right: 40px;
  }
  .carddiv {
    padding: 10px 30px;
  }
  .cardName {
    font-size: 18px;
    color: gray;
    margin-bottom: 20px;
  }
  .cardTime {
    font-size: 24px;
    color: #333;
    font-weight: bold;
  }
  .vipBox {
    display: flex;
    flex-wrap: nowrap;
  }
  // 背景卡片效果
  .card-feel {
    background-color: #eee;
    padding: 20px;
    margin: 10px 0px;
    border-radius: 10px;
    .avator {
      display: flex;
      width: 60px;
      height: 60px;
      align-items: center;
      img {
        border-radius: 50%;
        vertical-align: middle;
        margin-right: 10px;
      }
    }
    .info-item {
      display: inline-block;
      width: 300px;
      margin-top: 10px;
      .identification {
        display: inline-block;
      }
    }
    .gr {
      width: 500px;
    }
    // 更长
    .info-item-longer {
      width: 400px;
    }
    // 徽章那边的样式
    .badge-box {
      display: flex;
      align-items: center;
      margin-top: 20px;
      .badge-item {
        border: 1px solid #ddd;
        border-radius: 8px;
        padding: 5px 10px;
        display: flex;
        align-items: center;
        margin-right: 20px;
        .title {
          margin-right: 100px;
        }
        .created-at {
          margin-right: 20px;
        }
      }
    }
  }
  /deep/.el-input--mini .el-input__inner {
    height: 40px;
  }
  // /deep/.el-button--mini,
  // .el-button--mini.is-round {
  //   margin-left: 10px;
  // }
  /deep/ .el-date-editor--datetimerange {
    margin-bottom: 20px;
    margin-top: 10px;
  }
</style>
